package cn.niter.forum.provider;

import cn.niter.forum.dto.FileDTO;
import cn.niter.forum.model.User;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class LocalStorageProvider {

    @Value("${app.storage.local}")
    boolean appStorageLocal;

    @Value("${app.storage.dir}")
    String appStorageDir;

    @Value("${app.site}")
    String appSite;

    public boolean getAppStorageLocal() {
        return appStorageLocal;
    }

    public FileDTO upload(HttpServletRequest request, List<MultipartFile> multipartFiles) throws Exception {
        User user = (User) request.getSession().getAttribute("user");
        String[] data = new String[multipartFiles.size()];
        int count = 0;
        for (MultipartFile file : multipartFiles) {
            String contentType = file.getContentType();
            String fileName = getFileName(contentType);
            String userFilePath = getUserFilePath(user, contentType, fileName);
            String key = appStorageDir + userFilePath;
            File localFile = new File(key);
            if (!localFile.exists()) {
                localFile.mkdirs();
            }
            file.transferTo(localFile);
            String url = appSite + userFilePath;
            data[count] = url;
            count++;
        }
        FileDTO fileDTO = new FileDTO();
        fileDTO.setErrno(0);
        fileDTO.setData(data);
        return fileDTO;
    }

    public Map<String, Object> uploadLayImage(HttpServletRequest request, MultipartFile file) throws Exception {
        User user = (User) request.getSession().getAttribute("user");
        String contentType = file.getContentType();
        String fileName = getFileName(contentType);
        String userFilePath = getUserFilePath(user, contentType, fileName);
        String key = appStorageDir + userFilePath;
        File localFile = new File(key);
        if (!localFile.exists()) {
            localFile.mkdirs();
        }
        file.transferTo(localFile);
        String url = appSite + userFilePath;

        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("data", url);
        return map;
    }

    public Map<String, Object> uploadAvatar(HttpServletRequest request, MultipartFile file) throws Exception {
        User user = (User) request.getSession().getAttribute("user");
        String contentType = file.getContentType();
        String fileName = getFileName(contentType);
        String userFilePath = getUserFilePath(user, contentType, fileName);
        String key = appStorageDir + userFilePath;
        File localFile = new File(key);
        if (!localFile.exists()) {
            localFile.mkdirs();
        }
        file.transferTo(localFile);
        String url = appSite + userFilePath;

        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("data", url);
        return map;
    }

    public String getUserFilePath(User user, String fileType, String fileName) {
        return "upload/user/" + user.getId() + "/" + fileType + "/" + fileName;
    }

    public String getFileName(String contentType) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String timeStr = sdf.format(new Date());
        String[] ss = contentType.split("/");
        String str = RandomStringUtils.random(5,
                "abcdefghijklmnopqrstuvwxyz1234567890");
        String name = timeStr + "_" + str + "." + ss[1];
        return name;
    }

}
